Hi people,
if(matches("([-+]*)([0-9]+),([-+]*)([0-9]+)",range)) {
int leftNumber = intOf(range[match 1] "" range[match 2])
int rightNumber = intOf(range[match 3] "" range[match 4])
}
celalbaba - Fri Apr 27 04:57:44 EDT 2012 |
Re: parsing interval or range I don't understand some of your examples, but assuming they are typos, and you have nested brackets, look up "Recursive decent parser". Adam |
Re: parsing interval or range SystemAdmin - Mon Apr 30 10:13:41 EDT 2012 I used recursion to handle brackets inside brackets inside brackets ... Actually "U" characters are not type-mistaken. They occur in range values to say "Union". Therefore, i ignored all characters between same level brackets, because they all mean Union. |
Re: parsing interval or range celalbaba - Fri May 18 04:53:15 EDT 2012
With regexp you can handle nested expressions if they have a fixed depth. So if you have maximum number of nested expression, you can most of the time embed it in a regular expression. However that regular expression becomes pretty complex, even if you only have one nesting level. Soon the 9 groups that you have in DXL regexp will not work any more. Therefore you need to write a parser for your expressions.
RangeExpression: Range [UNION Range]*
Range : List | DelimitedRange
List : OPENLIST ListItem [SEP ListItem]* CLOSELIST
DelimitedRange : OPENRANGE DIGIT SEP DIGIT CLOSERANGE
ListItem : DIGIT | Range
// regular expressions for the tokens
SEP : ","
DIGIT : "-?[0-9]+"
OPENRANGE : "[\[(]"
CLOSERANGE: "[\])]"
OPENLIST : "{"
CLOSELIST : "}"
UNION : "U"
bool parseList () {
pushPosition () // Store the current parse positin
if (!expectToken (OPENLIST)) { popPosition(); return false }
if (!parseRangeItem ()) { popPosition(); return false }
// parse the "[SEP RangeItem]*" part
while (true) {
pushPosition()
if (!expectToken(SEP)) { popPosition(); break }
if (!parseListItem()) { popPosition(); break }
forward ()
}
// parse the CLOSELIST token
if (!expectToken (CLOSELIST)) { popPosition(); return false }
forward()
return true
}
Mathias Mamsch, IT-QBase GmbH, Consultant for Requirement Engineering and D00RS
|
Re: parsing interval or range Mathias Mamsch - Fri May 18 07:31:39 EDT 2012
With regexp you can handle nested expressions if they have a fixed depth. So if you have maximum number of nested expression, you can most of the time embed it in a regular expression. However that regular expression becomes pretty complex, even if you only have one nesting level. Soon the 9 groups that you have in DXL regexp will not work any more. Therefore you need to write a parser for your expressions.
RangeExpression: Range [UNION Range]*
Range : List | DelimitedRange
List : OPENLIST ListItem [SEP ListItem]* CLOSELIST
DelimitedRange : OPENRANGE DIGIT SEP DIGIT CLOSERANGE
ListItem : DIGIT | Range
// regular expressions for the tokens
SEP : ","
DIGIT : "-?[0-9]+"
OPENRANGE : "[\[(]"
CLOSERANGE: "[\])]"
OPENLIST : "{"
CLOSELIST : "}"
UNION : "U"
bool parseList () {
pushPosition () // Store the current parse positin
if (!expectToken (OPENLIST)) { popPosition(); return false }
if (!parseRangeItem ()) { popPosition(); return false }
// parse the "[SEP RangeItem]*" part
while (true) {
pushPosition()
if (!expectToken(SEP)) { popPosition(); break }
if (!parseListItem()) { popPosition(); break }
forward ()
}
// parse the CLOSELIST token
if (!expectToken (CLOSELIST)) { popPosition(); return false }
forward()
return true
}
Mathias Mamsch, IT-QBase GmbH, Consultant for Requirement Engineering and D00RS
whoops, i messed up the parseList code. The 'RangeItem' must be renamed to 'ListItem':
bool parseList () {
pushPosition () // Store the current parse position
if (!expectToken (OPENLIST)) { popPosition(); return false }
if (!parseListItem ()) { popPosition(); return false }
// parse the "[SEP ListItem]*" part
while (true) {
pushPosition()
if (!expectToken(SEP)) { popPosition(); break }
if (!parseListItem()) { popPosition(); break }
forward () // success, consume the tokens
}
// parse the CLOSELIST token
if (!expectToken (CLOSELIST)) { popPosition(); return false }
forward() // success, list has been parsed. Consume the tokens.
return true
}
Mathias Mamsch, IT-QBase GmbH, Consultant for Requirement Engineering and D00RS
|
Re: parsing interval or range Mathias Mamsch - Fri May 18 07:31:39 EDT 2012
With regexp you can handle nested expressions if they have a fixed depth. So if you have maximum number of nested expression, you can most of the time embed it in a regular expression. However that regular expression becomes pretty complex, even if you only have one nesting level. Soon the 9 groups that you have in DXL regexp will not work any more. Therefore you need to write a parser for your expressions.
RangeExpression: Range [UNION Range]*
Range : List | DelimitedRange
List : OPENLIST ListItem [SEP ListItem]* CLOSELIST
DelimitedRange : OPENRANGE DIGIT SEP DIGIT CLOSERANGE
ListItem : DIGIT | Range
// regular expressions for the tokens
SEP : ","
DIGIT : "-?[0-9]+"
OPENRANGE : "[\[(]"
CLOSERANGE: "[\])]"
OPENLIST : "{"
CLOSELIST : "}"
UNION : "U"
bool parseList () {
pushPosition () // Store the current parse positin
if (!expectToken (OPENLIST)) { popPosition(); return false }
if (!parseRangeItem ()) { popPosition(); return false }
// parse the "[SEP RangeItem]*" part
while (true) {
pushPosition()
if (!expectToken(SEP)) { popPosition(); break }
if (!parseListItem()) { popPosition(); break }
forward ()
}
// parse the CLOSELIST token
if (!expectToken (CLOSELIST)) { popPosition(); return false }
forward()
return true
}
Mathias Mamsch, IT-QBase GmbH, Consultant for Requirement Engineering and D00RS
Thank you Mathias,
//state 0: unknown, 1: leftOpen (, 2: leftClosed [, 3: rightOpen ), 4: rightClosed, 5: element {}
int parseInterval(string range, int state) {
for(i = 0; i < length range; i++) {
if(currentChar == '[' || '(') {
findClosingBracket() //']' or ')'
handleBracketInside()
shiftIteratorAfterBracket()
}
if(state == ELEMENT)
getNumbersFromCurlyBracketsTokenizedByComma()
if(currentChar == '{') {
findClosingBracket() // '}'
parseInterval(subRangeWithoutBraces, ELEMENT)
parseInterval(restOfRange, UNKNOWN)
}
}
}
|